﻿using System.Windows;
using Baosight.ZNH.ZMCommon;
using System;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI;
using Microsoft.Practices.Unity;
using Baosight.ZNH.ZMCS.UI.Utils;
using System.Collections.Generic;
using System.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCS.UI.ChildWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCS.UI.Download;

namespace Baosight.ZNH.ZMCS.UI
{
    public partial class ZMCS81 : EFViewCmes
    {
        private EngineeringBlock _block = new EngineeringBlock();//私有化下拉列表
        private string _type = "";

        public ZMCS81()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(EIInfo initData)
        {
            initData.AddBlock(_block.GetKeyProject(true));//重点工程
            initData.AddBlock(_block.GetEngineeringStatus(true));//工程状态
            initData.AddBlock(_block.GetEngineeringType(false));//工程类别
            string parentTypes = initData.Get("parentTypes") == null ? "" : initData.Get("parentTypes").ToString();

            if ("0".Equals(parentTypes))
            {
                initData.AddBlock(_block.GetEngineeringClassCM(true));//采煤类别
            }
            else if ("1".Equals(parentTypes))
            {
                initData.AddBlock(_block.GetEngineeringClassJJ(true));//掘进类别
            }
            else if ("2".Equals(parentTypes))
            {
                initData.AddBlock(_block.GetEngineeringClassTF(true));//通防类别
            }
            else
            {
                initData.AddBlock(_block.GetEngineeringClassQT(true));//其他类别
            }

            base.Initialize(initData);

            string childTypes = initData.Attributes.ContainsKey("childTypes") ? initData.Get("childTypes").ToString() : ""; 
            if (!"".Equals(childTypes))
            {
                this.INQU_fdClass.IsEnabled = false;
                this.INQU_fdClass.Value = childTypes;
            }
        }

        /// <summary>
        /// 点击新增按钮，根据矿区形式判断是否可以新增矿井记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "工程-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            List<EIRow> _rows = this.grid.CheckedEIRows;
            int count = _rows.Count;
            if (count <= 0)
            {
                BaseTools.OpenEFWindowAlert("请选择一条记录进行修改！", WindowAlertClosed);
                return;
            }
            else if (count > 1)
            {
                BaseTools.OpenEFWindowAlert("最多只能选择一条记录进行修改！", WindowAlertClosed);
                return;
            }
            else
            {
                string fdStatus = _rows[0]["fdStatus"] == null ? "" : _rows[0]["fdStatus"].ToString();
                if (!"0".Equals(fdStatus))
                {
                    base.OpenEFWindowAlert("只能修改状态为设计的工程记录！", true);
                    return;
                }
                EIInfo info = new EIInfo();
                info.Attributes.Add(EIConstants.ServiceNameKey, "ZMCS81");
                info.Attributes.Add(EIConstants.MethodNameKey, "queryFiles");
                info.Attributes.Add("fdKeyId", _rows[0]["fdKeyId"]);

                HttpServiceAgent agent = new HttpServiceAgent();
                agent.CallService(info, OnQueryFilesCompleted);
            }
        }

        /// <summary>
        /// 修改按钮中查询附件信息的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnQueryFilesCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status != -1)
            {
                IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();

                EIInfo eiInfo = new EIInfo();
                EIBlock eIBlock = new EIBlock("result");
                EIRow row = new EIRow();
                if (_list.Count > 0)
                {
                    row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
                }
                eIBlock.SetRow(0, row);
                eiInfo.AddBlock(eIBlock);
                eiInfo.AddBlock(e.EIInfo.GetBlock("filesresult"));

                base.OnUpdateButtonClickPopup(eiInfo, "工程-修改", "update", null, TempButton, null);
            }
            else
            {
                BaseTools.OpenEFWindowAlert("查询附件失败！", null);
                this.TempButtonOpen(true);
            }
        }

        /// <summary>
        /// 点击查看按钮执行的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            _type = row["fdType"] == null ? "" : row["fdType"].ToString();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "工程-查看", "detail", null, sender, e);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            foreach (EIRow row in _list)
            {
                string fdStatus = row["fdStatus"] == null ? "" : row["fdStatus"].ToString();
                if (!"0".Equals(fdStatus))
                {
                    base.OpenEFWindowAlert("只能删除状态为计划的工程记录！", true);
                    return;
                }
            }
            base.OnDeleteButtonClick(sender, e);
        }

        /// <summary>
        /// 点击开工事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnModifyStartButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");

            EIRow row = new EIRow();
            if (_list.Count > 1)
            {
                base.OpenEFWindowAlert("只能选择一条工程记录进行开工操作！", true);
                return;
            }
            else if (_list.Count <= 0)
            {
                base.OpenEFWindowAlert("请选择工程记录！", true);
                return;
            }
            else
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
                string fdStatus = row["fdStatus"] == null ? "" : row["fdStatus"].ToString();
                if ("1".Equals(fdStatus))
                {
                    base.OpenEFWindowAlert("该工程已经开工！", true);
                    return;
                }
                else if ("2".Equals(fdStatus))
                {
                    base.OpenEFWindowAlert("已经竣工的工程不能再标记为开工！", true);
                    return;
                }
            }

            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnInsertButtonClickPopup(eiInfo, "工程-开工", "update", "modifyStart", sender, e);
        }

        /// <summary>
        /// 点击竣工事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnModifyStopButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnModifyButtonClick("modifyStop", "竣工", sender, e);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            //工程开工
            if (null != _args && "modifyStart".Equals(_args))
            {
                OpenPopupStart(_eIInfo, _title, _dialogType, _args);
            }
            else
            {
                OpenPopupInsert(_eIInfo, _title, _dialogType, _args);
            }

        }

        /// <summary>
        /// 弹出窗口关闭后执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                this.grid.Refresh();//刷新Grid区域
                this.UpdateHomePageData(new EIInfo());//发布数据刷新树
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 点击下载附件按钮执行的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DownLoad_HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            TempHyperlink = sender as HyperlinkButton;
            TempHyperlink.IsEnabled = false;
            EIRow row = (TempHyperlink.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo info = new EIInfo();
            info.Attributes.Add(EIConstants.ServiceNameKey, "ZMCS81");
            info.Attributes.Add(EIConstants.MethodNameKey, "queryFiles");
            info.Attributes.Add("fdKeyId", row["fdKeyId"]);

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(info, OnQueryDownloadFilesCompleted);
        }

        /// <summary>
        /// 下载附件按钮中查询附件的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnQueryDownloadFilesCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            DownloadView view = new DownloadView();
            view.EIDataContext = new Platform.Core.UI.EIDataContext();
            view.EIDataContext.Update(e.EIInfo);

            CmesWindows _win = new CmesWindows();//构造一个弹出窗口
            _win.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _win.Header = view.eTitle;//设置Title
            _win.Content = view;
            _win.Closed += delegate { if (TempHyperlink != null) { TempHyperlink.IsEnabled = true; TempHyperlink = null; } };
            _win.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 新增\修改时
        /// </summary>
        /// <param name="_eIInfo"></param>
        /// <param name="_title"></param>
        /// <param name="_dialogType"></param>
        /// <param name="_args"></param>
        private void OpenPopupInsert(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            Engineering _project = new Engineering(_dialogType);

            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.specialFund"));//专项资金
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.engineeringGroup"));//工程组
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.constructionUnit"));//施工部门
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.engineeringParentId"));//工程所属
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.holeType"));//钻孔类型
            _eIInfo.AddBlock(_block.GetKeyProject(true));//重点工程
            _eIInfo.AddBlock(_block.GetEngineeringStatus(false));//工程状态

            string parentTypes = this.parentTypes.Value == null ? "" : this.parentTypes.Value.ToString().Trim();//大类
            string childTypes = this.childTypes.Value == null ? "" : this.childTypes.Value.ToString().Trim();//小类

            if (_dialogType.Equals("insert"))
            {

                if ("0".Equals(parentTypes))
                {
                    //采煤
                    _eIInfo.AddBlock(_block.GetEngineeringClassCM(false));//采煤分类
                    _project.fdType.Value = "0";
                    _project.fdUnit.IsReadOnly = true;
                }
                else if ("1".Equals(parentTypes))
                {
                    //掘进
                    _eIInfo.AddBlock(_block.GetEngineeringClassJJ(false));//掘进分类
                    _project.fdType.Value = "1";
                    _project.fdUnit.IsReadOnly = true;
                }
                else if ("2".Equals(parentTypes))
                {
                    //通防
                    _eIInfo.AddBlock(_block.GetEngineeringClassTF(false));//通防分类
                    _project.fdType.Value = "2";

                    //打钻类型的工程 需要显示打钻类型
                    if (null != childTypes && "打钻".Equals(childTypes))
                    {
                        _project.fdExt1.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        _project.fdExt1.Visibility = Visibility.Collapsed;
                    }
                }
                else
                {
                    //其他
                    _eIInfo.AddBlock(_block.GetEngineeringClassQT(false));//其他分类
                    _project.fdType.Value = "3";
                }
                if (!"".Equals(childTypes))
                {
                    _project.fdClass.Value = childTypes;
                    _project.fdClass.IsEnabled = false;
                    if ("0".Equals(parentTypes))
                    {
                        _project.fdUnit.Value = "吨";
                    }
                    else if ("1".Equals(parentTypes))
                    {
                        _project.fdUnit.Value = "米";
                    }
                    else if ("2".Equals(parentTypes))
                    {
                        if ("打钻".Equals(childTypes))
                        {
                            _project.fdUnit.Value = "米";
                        }
                        else if ("抽采".Equals(childTypes))
                        {
                            _project.fdUnit.Value = "万立方米";
                        }
                        else if ("管路安装".Equals(childTypes))
                        {
                            _project.fdUnit.Value = "米";
                        }
                        else
                        {
                            _project.fdUnit.Value = "个";
                        }
                    }
                    else
                    {
                        _project.fdUnit.IsReadOnly = false;
                    }
                }
            }
            else
            {
                if ("0".Equals(parentTypes))
                {
                    //采煤
                    _eIInfo.AddBlock(_block.GetEngineeringClassCM(false));//采煤分类
                }
                else if ("1".Equals(parentTypes))
                {
                    //掘进
                    _eIInfo.AddBlock(_block.GetEngineeringClassJJ(false));//掘进分类
                }
                else if ("2".Equals(parentTypes))
                {
                    //通防
                    _eIInfo.AddBlock(_block.GetEngineeringClassTF(false));//通防分类
                    string fdClass = _eIInfo.GetBlock("result").GetRow(0)["fdClass"] == null ? "" : _eIInfo.GetBlock("result").GetRow(0)["fdClass"].ToString();
                    if ("打钻".Equals(fdClass))
                    {
                        _project.fdExt1.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        _project.fdExt1.Visibility = Visibility.Collapsed;
                    }
                }
                else
                {
                    //其他
                    _eIInfo.AddBlock(_block.GetEngineeringClassQT(false));//其他分类
                }
            }

            _project.EIDataContext = new Platform.Core.UI.EIDataContext();
            _project.EIDataContext.Update(_eIInfo);

            _project.engineeringNoType.Value = this.engineeringNoType.Value.ToString();//编码格式

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title;//设置Title
            _popup.Content = _project;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 开工
        /// </summary>
        /// <param name="_eIInfo"></param>
        /// <param name="_title"></param>
        /// <param name="_dialogType"></param>
        /// <param name="_args"></param>
        private void OpenPopupStart(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            EngineeringStart _project = new EngineeringStart(_dialogType);

            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.constructionUnit"));//施工部门

            _project.EIDataContext = new Platform.Core.UI.EIDataContext();
            _project.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title;//设置Title
            _popup.Content = _project;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }
    }
}
